// component/auth/requestAuthorize.js
const cache = require("../../utils/cache")
const app = getApp()
Component({
  options:{
    styleIsolation: 'isolated'
  },
  /**
   * 组件的属性列表oyJUs5LQwM4W2tWBhcYNGGveHJxI
   */
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {

  },

  /**
   * 组件的方法列表
   */
  methods: {
    showModal(e) {
      this.setData({
        modalName: 'bottomModal'
      })
    },
    hideModal(e) {
      this.setData({
        modalName: null
      })
    },
    handleAuthorize(){
      let that = this
      wx.authorize({
        scope: 'scope.userInfo',
        success(){
          wx.getSetting({
            success: res => {
              if (res.authSetting['scope.userInfo']) {
                // 已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框
                wx.getUserInfo({
                  success: res => {
                    // 可以将 res 发送给后台解码出 unionId
                    console.log(res.userInfo)
                    app.globalData.userInfo = res.userInfo
                    cache.setValue("scope.userInfo",app.globalData.userInfo)
                    that.triggerEvent('bindFunc',null)
                  }
                })
              }
            }
          })
        }
      })
    }
  },
  attached:function(){
    let userInfo = cache.getValue("scope.userInfo")
    if(userInfo== undefined || userInfo==null || userInfo==""){
      this.setData({
        modalName: 'bottomModal'
      })
    }
  }
})
